﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class productproperty_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormInt("id") > 0)
                {
                    string types = Request.Form["Option"].Trim();
                    int id = ChangeHope.WebPage.PageRequest.GetFormInt("id");
                    int propertyId = ChangeHope.WebPage.PageRequest.GetFormInt("propertyId");
                    if (types == "del")
                    {
                        del(id,propertyId);
                    }
                    Response.End();
                    return;
                }
                string classValue = ChangeHope.WebPage.PageRequest.GetString("classValue");
                if (classValue != "")
                {
                    ViewState["ClassValue"] = classValue;
                }
                if (ChangeHope.WebPage.PageRequest.GetString("id") != "")
                {
                    string propertyId=ChangeHope.WebPage.PageRequest.GetString("id");
                    ViewState["PropertyId"] = propertyId;
                    this.HyperLink2.NavigateUrl = "productproperty_edit.aspx?classValue=" + ViewState["ClassValue"] + "&propertyId=" + propertyId;
                    this.Literal1.Text = GetList(Convert.ToInt32(propertyId));
                }
               
            } 
            
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        protected string GetList(int productTypeId)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Product.ProductType data = new YXShop.BLL.Product.ProductType();
            ChangeHope.DataBase.DataByPage dataPage = data.GetProductPropertyList(productTypeId);
            //第一步先添加表头
            table.AddHeadCol("5%", "序号");
            table.AddHeadCol("25%", "属性名称");
            table.AddHeadCol("35%", "所属值");
            table.AddHeadCol("15%", "所属类型");
            table.AddHeadCol("10%", "排序");
            table.AddHeadCol("10%", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol(No);
                    table.AddCol(dataPage.DataReader["PropertyName"].ToString());
                    table.AddCol("<span title='" + dataPage.DataReader["PropertyValue"].ToString().Replace("\r\n", ",") + "'>" +ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["PropertyValue"].ToString().Replace("\r\n", ","),18) + "</span>");
                    table.AddCol(Type(dataPage.DataReader["PropertyType"].ToString()));
                    table.AddCol(string.Format("<input id='sort{0}' value='{1}' size='3' onblur='Sort({0},{1})'/>", dataPage.DataReader["id"].ToString(), dataPage.DataReader["Sort"].ToString()));
                    table.AddCol(string.Format("<a href=productproperty_edit.aspx?classValue=" + ViewState["ClassValue"] + "&id={0}&propertyId={1}>编辑</a> <a href='#' onclick='Del({0},{1})'>删除</a>", dataPage.DataReader["id"].ToString(), ViewState["PropertyId"]));

                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        protected string Type(string value)
        {
            string reStr = string.Empty;
            switch (value)
            {
                case "1":
                    reStr = "下拉列表";
                    break;
                case "2":
                    reStr = "单选";
                    break;
                case "3":
                    reStr = "多选";
                    break;
                case "4":
                    reStr = "单行文本";
                    break;
                case "5":
                    reStr = "多行文本";
                    break;
            }
            return reStr;
        }
        private void del(int id, int propertyId)
        {
            YXShop.BLL.Product.ProductType bll = new YXShop.BLL.Product.ProductType();
            bll.DeleteProductProperty(id, propertyId);
            Response.Write("ok");
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("product_type_edit.aspx?backType=Set&id=" + ViewState["PropertyId"] + "");
        }

        protected void button4_Click(object sender, EventArgs e)
        {
            if (ViewState["ClassValue"]!=null)
            {
                string classValue = ViewState["ClassValue"].ToString();
                if (classValue.Contains("3"))
                {
                    Response.Redirect("productparameter_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["PropertyId"] + "");
                }
                else if (classValue.Contains("1"))
                {
                    Response.Redirect("productother_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["PropertyId"] + "");
                }
                else if (classValue.Contains("4"))
                {
                    Response.Redirect("productbuyset_list.aspx?classValue=" + ViewState["ClassValue"] + "&id=" + ViewState["PropertyId"] + "");
                }
                else
                {
                    ChangeHope.WebPage.BasePage.PageRight("保存成功。", "product_type_list.aspx");
                }
            }
            else
            {
                ChangeHope.WebPage.BasePage.PageRight("保存成功。", "product_type_list.aspx");
            }
        }
        
    }
}
